#!/usr/bin/env ruby

# https://github.com/scheduler-tools/rt-app/blob/master/doc/tutorial.txt

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/statistics"
require "#{LKP_SRC}/lib/log"

task = nil
run = []
period = []
slack = []
wu_lat = []

def puts_one_task(task, run, period, slack, wu_lat)
  return if task.nil?

  if run.size.positive?
    # first 2 line data unstable observed sometimes.
    # could consider to skip first 2 line to wait stable data
    # TODO
    puts "LAT.#{task}.run.us: #{run.average}"
    puts "JIT.#{task}.run.stddev: #{run.standard_deviation}"
    puts "LAT.#{task}.period.us: #{period.average}"
    puts "JIT.#{task}.period.stddev: #{period.standard_deviation}"
    puts "LAT.#{task}.slack.us: #{slack.average}"
    puts "JIT.#{task}.slack.stddev: #{slack.standard_deviation}"
    puts "LAT.#{task}.wu_lat.us: #{wu_lat.average}"
    puts "JIT.#{task}.wu_lat.stddev: #{wu_lat.standard_deviation}"
  else
    # sometimes empty data for some tasks which is expected
    # just warning here
    log_error 'stats/rt-app: invalid data'
    exit 1
  end
end

while (line = $stdin.gets)
  case line
  when /(.*).log$/
    puts_one_task(task, run, period, slack, wu_lat)
    task = $1.gsub('./', '')
    run.clear
    period.clear
    slack.clear
    wu_lat.clear
  when /(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)/
    next unless task

    run << $5.to_i
    period << $7.to_i
    slack << $15.to_i
    wu_lat << $21.to_i
  end
end

puts_one_task(task, run, period, slack, wu_lat)
